import {SERVER} from "../configs";
import {roomuser} from "./roomuser";
import {Message} from "./message";
export  class Rooms
{

     roomname:string="";
     personall:number=0;
     persononline:number=0;
     owner:string | undefined="";

     create():Promise<Boolean>
     {
         return fetch(SERVER+"/createRoom",{
             method:"POST",
             headers:{
                 "Content-Type":"application/json"
             },
             body:JSON.stringify({
                 roomname:this.roomname,
                 personall:this.personall,
                 persononline:this.persononline,
                 owner:this.owner
             })
         }).then(res=>{

             if(res.status===200)
                 return true;
             else  return false;
         })
     }

    join(r1: string, username: string | undefined):Promise<Boolean>
     {
         return fetch(SERVER+"/joinRooms",{
             method:"POST",
             headers:{
                 "Content-Type":"application/json"
             },

             body:JSON.stringify({
                 roomname:r1,
                 name:username,
                 online:1,
             })
         }).then(res=>{
             if(res.status===200)
                 return true;
             else  return false;
         })
     }
    getuser():Promise<roomuser[]>{
        return fetch(SERVER+"/roomuser/getUsers?roomid="+this.roomname,{
            method:"GET"
        }).then(res=>res.json());
    }
    static getmessage(roomname:string):Promise<Message[]>{

        return fetch(`${SERVER}/message/getMessages?roomname=${roomname}`, {
            method: "GET",
            headers: {
                "Content-Type": "application/json"
            },
        }).then(res => res.json());
    }
    show(username:string):Promise<Rooms[]> {
        return fetch(SERVER+"/showRooms",{
            method:"POST",
            headers:{
                "Content-Type":"application/json"
            },
            body:JSON.stringify({
                owner:username
            })
        }).then(res=>{
            if(res.status===200) {
                return res.json();
            }
            else  return [];
        })
    }

}
